package gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JLabel;

/**
 * @author Ramy Refky 
 * */
public class ModuleComponentLabel extends ComponentLabel{

	private static final long serialVersionUID = 1L;
	
	JLabel moduleLabel;
	
	BufferedImage image;
	LoadDataHandler ld;
	boolean showDetails;
	
	/**
	 * Constructor initializes ComponentLabel. A LoadHandler instance is used to retrieve an image
	 * representation of the loaded file. 
	 * @param XLocation, YLocation, Width, Height, LoadHandler, FileName, ShowDetails
	 */
	public ModuleComponentLabel(int xLoc,int yLoc,int originalWidth, int originalHeight,
			LoadDataHandler ld,String name,boolean showDetails){
		super(xLoc,yLoc,originalWidth,originalHeight);

		this.ld=ld;
		this.showDetails=showDetails;
		
		if(showDetails){
			this.image=ld.loadModule();
		}
		moduleLabel = new JLabel(name,JLabel.CENTER);
		moduleLabel.setBounds(0,0,newWidth,15);
		add(moduleLabel);
		
		repaint();
	}
	@Override
	public void zoomAdjust(Double zoomFactor){
		newWidth=(int) (originalWidth*zoomFactor);
		newHeight=(int) (originalHeight*zoomFactor);
		setBounds(0,0,newWidth,newHeight);
		moduleLabel.setBounds(0,0,newWidth,15);
		if(showDetails){
			image=ld.loadModule();
		}
		repaint();
	}
	/**
	 * Resizes a given image to match given dimensions, and the resized image is returned.
	 * @return BufferedImage
	 */
	public BufferedImage resizeImage(BufferedImage img, int newW, int newH) {  
       
		int w = img.getWidth();  
        int h = img.getHeight();  
        
        int fixedW=newW;
        int fixedH=newH;
        
        if(w>h){
        	double ratio = (h+0.0)/(w+0.0);
        	if((int)((newH+0.0)/(newW+0.0))!=(int)((h+0.0)/(w+0.0))){
        		fixedH=(int) (newH*ratio);
        	}
        }else{
        	if(w<h){
        		double ratio = (w+0.0)/(h+0.0);
        		if((int)((newW+0.0)/(newH+0.0))!=(int)((w+0.0)/(h+0.0))){
            		fixedW=(int) (newW*ratio);
            	}
        	}
        }
        BufferedImage dimg = new BufferedImage(newW, newH, img.getType());  
        Graphics2D g = dimg.createGraphics();  
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
        g.drawImage(img, 0, 0, fixedW, fixedH, 0, 0, w, h, null);  
        g.dispose();  
        return dimg;  
    }  
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		Rectangle Box = new Rectangle();
		Box.setBounds(newWidth/6,2,newWidth*2/3,newHeight-4);
		
    	Graphics2D g2 = (Graphics2D)g;
    	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);
    	
        g2.setStroke(new BasicStroke(2f));
        g2.setPaint(Color.black);
        g2.draw(Box);
        
        if(!selected)
        	g2.setPaint(Color.white);
        else
        	g2.setPaint(Color.getHSBColor((float)0.5,(float)0.1,(float)0.9));
        Box.setBounds(newWidth/6+1,3,newWidth*2/3-1,newHeight-5);
        
        g2.fill(Box);
        g2.setPaint(Color.black);
       
        if(showDetails){
        	image=resizeImage(image,(int)((newWidth*2/3-1)*0.75),(int)((newHeight-5)*0.75));
            g.drawImage(image, newWidth/4+1,newHeight/5, this);
        }
	}
}
